/**
 * Created by sxwu on 14-2-15.
 */


var fields_all = "userid,wifiid,apid,apmac,apsn,apdes,aplocation,apcustomernum,apflag";

/**
 * 模块依赖
 */
var db = require("../lib/db");



//查询ap是否存在,直接更新状态更新成功注册成功,更新失败返回注册失败
exports.selectMac = function(apmac,handler)
{
    db.execQuery({
            //'sql':"update virapinfo set apflag = 1 where apmac=?",
            'sql':"select * from virapinfo where apmac =?",
            'args':[apmac],
            'handler':handler
        }
    );
}

//查找记录
exports.selectAPWiFi = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.* FROM virapinfo a, virwifiinfo b WHERE a.wifiid = b.wifiid and  a.userid=? and b.userid=?",
        "args": [userid,userid],
        "handler": handler
    });

};

//查找记录
exports.selectAPWiFiWithAPMac = function(apmac, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.* FROM virapinfo a, virwifiinfo b WHERE a.wifiid = b.wifiid and  a.apmac=?",
        "args": [apmac],
        "handler": handler
    });
};

exports.selectForPassthrough = function(whereclause, handler) {
    db.execQuery({
       /* "sql": "SELECT a.*,b.* FROM virapinfo a, customerinfo b WHERE " +
            "a.wifiid = b.wifiid " +
            "and  a.apmac=? and b.custmac=? and b.logdowndate=null",*/
        /*select a.*,b.* from virapinfo a, customerinfo b where a.apmac=b.apmac and a.apmac=? and b.custmac=?*/
        //先根据custmac查找apmac，根据apmac查找groupforssid，如果groupforssid和当前的ap相同 且custmac没有下线认为找到了
        "sql": "select a.*,b.* from virapinfo a,virwifiinfo b where apmac in (select apmac from customerinfo where custmac=? and custflag=1) " +
            "and groupenable=1 " +
            "and groupidforssid =  (select groupidforssid from virapinfo where apmac=?) " +
            " and a.wifiid = b.wifiid",
        "args": [
            whereclause.usermac,
            whereclause.apmac

        ],
        "handler": handler
    });
};


//查找记录
exports.select = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT * FROM virapinfo WHERE userid=?",
        "args": [userid],
        "handler": handler
    });

};

exports.selectDevidBySSID = function(apinfo, handler) {
    db.execQuery({
        "sql": "SELECT * FROM virapinfo WHERE userid=? and apmac=? and groupidforssid=? and devid=? and apid!=?",
        "args": [apinfo.userid,
        apinfo.groupidforssid,
        apinfo.devid,
        apinfo.apid],
        "handler": handler
    });

};

//插入记录
exports.insert = function(apinfo, handler) {
    db.execQuery({
       // "sql": "insert into virapinfo(userid,wifiid,apmac,apsn,apdes,aplocation,apcustomernum,apflag) values(?,?,?,?,?,?,?,?)",
        "sql": "insert into virapinfo(userid,wifiid,apname,apmac,apsn,apcustomernum,apflag," +
            "updatetime,groupenable,devid,groupidforssid) values(?,?,?,?,?,0,0,now(),0,0,'0')",
        "args":[ apinfo.userid,
            apinfo.wifiid,
            apinfo.apname,
            apinfo.apmac,
            apinfo.apsn
           // apinfo.apcustomernum,
           // apinfo.apflag
           ],
        "handler": handler
    });
};

//更新记录
exports.update = function(apinfo, handler) {
    db.execQuery({
        "sql": "update virapinfo set apname=?,apmac=?,apsn=?," +
            " groupenable=?,devid=?,groupidforssid=?  WHERE apid=?",
        "args": [//apinfo.wifiid,
            apinfo.apname,
            apinfo.apmac,
            apinfo.apsn,


            apinfo.groupenable,
            apinfo.devid,
            apinfo.groupidforssid,

            apinfo.apid],
        "handler": handler
    });

};
/*
 //插入记录
 exports.insert = function(apinfo, handler) {
 db.execQuery({
 // "sql": "insert into virapinfo(userid,wifiid,apmac,apsn,apdes,aplocation,apcustomernum,apflag) values(?,?,?,?,?,?,?,?)",
 "sql": "insert into virapinfo(userid,wifiid,apname,apmac,apsn,apdes,aplocation,apcustomernum,apflag," +
 "updatetime,groupenable,devid,groupidforssid) values(?,?,?,?,?,?,?,0,0,now(),0,0,'0')",
 "args":[ apinfo.userid,
 apinfo.wifiid,
 apinfo.apname,
 apinfo.apmac,
 apinfo.apsn,
 apinfo.apdes,
 apinfo.aplocation
 // apinfo.apcustomernum,
 // apinfo.apflag
 ],
 "handler": handler
 });
 };

 //更新记录
 exports.update = function(apinfo, handler) {
 db.execQuery({
 "sql": "update virapinfo set apname=?,apmac=?,apsn=?,apdes=?,aplocation=?," +
 " groupenable=?,devid=?,groupidforssid=?  WHERE apid=?",
 "args": [//apinfo.wifiid,
 apinfo.apname,
 apinfo.apmac,
 apinfo.apsn,
 apinfo.apdes,
 apinfo.aplocation,

 apinfo.groupenable,
 apinfo.devid,
 apinfo.groupidforssid,

 apinfo.apid],
 "handler": handler
 });

 };
* */

//更新记录
exports.updateapflag = function(apinfo, handler) {
    db.execQuery({
        "sql": "update virapinfo set apflag=?,updatetime=now() where apmac=?",
        "args": [
            apinfo.apflag,
            apinfo.apmac],
        "handler": handler
    });

};


//删除记录
exports.delete = function(apid, handler) {
    db.execQuery({
        "sql": "delete from  virapinfo  WHERE apid in (?)",
        "args": [apid],
        "handler": handler
    });

};

//遍历记录
exports.findAll = function(handler) {
    db.execQuery({
        "sql": "SELECT * FROM virapinfo ORDER BY apid asc",
        "handler": handler
    });
};
